<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ReportController.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


class Mall_UserreportController extends Baogg_Controller_Action {

	function init() 
	{		
		Baogg_Language::loadFile("user");
	}
	
	function listAction(){
		
		$params=$this->getRequest()->getParams();
		$period=5;
		$arr_year=array();
		$cur_year=(int)date("Y");
		$period=5;
		for($i=$cur_year;$i>$cur_year-$period;$i--){
			$arr_year[]=$i;
		}

		if(isset($params['user_id'])){
			$url_param['user_id']=$params['user_id'];
		}else{
			$url_param['user_id']=$_SESSION['admin']['user']['user_id'];
		}
		if(isset($params['begin_time'])){
			$url_param['begin_time']=(int)$params['begin_time'];
		}else{
			$url_param['begin_time']=(int)date("Y");
		}
		if(isset($params['model_id'])){
			$url_param['model_id']=$params['model_id'];
		}
		
		

		$arr_url=$params;
		$arr_url['action']='data';
		$arr_url=array_merge($arr_url,$url_param);
		$arr_user_url=array("module"=>"system","controller"=>"user","action"=>"store");
		
		$cur_year=(int)date("Y");
		$arr_year=array();
		for($i=$cur_year;$i>$cur_year-$period;$i--){
			$arr_year[]=$i;
		}
		

		
		
		
		$this->view->url=Baogg_Controller_Url::generate($arr_url);
		$arr_url['action']='download';
		$this->view->download_url=Baogg_Controller_Url::generate($arr_url);
		$this->view->user_url=Baogg_Controller_Url::generate($arr_user_url);
		//echo '<pre>';print_r($arr_user_url);var_dump(Baogg_Controller_Url::generate($arr_url));exit;
		$this->view->user_id=$url_param['user_id'];
		
		$this->view->begin_time_data=Zend_Json::encode($arr_year);
		$this->view->begin_time=$url_param['begin_time'];
		$this->view->arr_year=Zend_Json::encode($arr_year);
		$this->view->action=$params['action']."content";
		
	}

	function dataAction(){
		$params=$this->getRequest()->getParams();

		

		if(isset($params['user_id'])){
			$user_id=$params['user_id'];
		}else{
			$user_id=$_SESSION['admin']['user']['user_id'];
		}
		if(isset($params['begin_time'])){
			$begin_time=$params['begin_time'];
		}else{
			$begin_time=date("Y");
		}

		$User=new User();
		$rs_user=$User->getOne($user_id);	
		
		$gender=(int)@$rs_user[0]['gender'];
		$arr_gender=BAOGG_LANGUAGE::get('arr_gender');
		isset($arr_gender[$gender]) && $rs_user[0]['gender']=$arr_gender[$gender];
		
		$education=(int)@$rs_user[0]['education'];
		$arr_education=BAOGG_LANGUAGE::get('arr_education');
		isset($arr_education[$education]) && $rs_user[0]['education']=$arr_education[$education];
		
		$user_group_id=(int)@$rs_user[0]['user_group_id'];
		$UserGroup=new UserGroup();
		$store_user_group_id=$UserGroup->getArray(array("user_group_id"=>$user_group_id),NULL,NULL,array("user_group_id","user_group_name","user_group_pid"));
		isset($store_user_group_id[$user_group_id]) && $rs_user[0]['user_group_id']=$store_user_group_id[$user_group_id]['user_group_name'];
		//echo '<pre>'.__FILE__.__LINE__;print_r($arr_gender);exit;	
		
		$this->view->user=@$rs_user[0];
		
		$Model=new Model();
		$arr_model=array();
		$dir=BAOGG_UPLOAD_DIR."rule/";
		$rs_model=$Model->getList(array("state"=>1,"main_table"=>"user"));
		$total_score=0;
		foreach((array)$rs_model as $schema){
			$arr_model[$schema['ext_table']]=array("name"=>$schema['model_name'],"score"=>0,"demo"=>"","items"=>array());
			$ModelTable=new ModelTable((int)$schema['model_id']);
			$rs_model_table=$ModelTable->getShowList(array("user_id"=>(int)$user_id,"begin_time"=>$begin_time."-%"));
			foreach((array)$rs_model_table as $k=>$v){
				$file=$dir.$schema['rule_id'].".php";
				$v['score']=include $file;
				$total_score+=$v['score'];

				$ModelTable->_update(array("score"=>$v['score']),$v['id']);

				$arr_model[$schema['ext_table']]['score']+=$v['score'];
				$arr_model[$schema['ext_table']]['demo'] .= @$v['demo'].";";
				$arr_model[$schema['ext_table']]['items'][]=$v;			
			}
		}
		foreach((array)$arr_model as $k=>$v){
			$this->view->$k=(array)$v;
		}

		$this->view->total_score=$total_score;
		global $LANG;
		$this->view->LANG=$LANG;
		//echo '<pre>';print_r($arr_model);exit;
	}
	
    function downloadAction(){
	    //echo "<pre>";print_r($_SERVER);exit;
	    $url=$this->getRequest()->getParams();
	    $url['action']='data';
        
	    ob_start();
	    $this->dataAction();
	    $this->view->getEngine()->display($url['module'].'/'.$url['controller'].'_'.$url['action'].'.tpl');
	    $html=ob_get_contents();
	    ob_end_clean();
	    //echo $html;exit;
	    Baogg_File::genPdfByContent($html,'Userreport');
	    Baogg_File::download(BAOGG_UPLOAD_DIR.'pdf/Userreport.pdf');
	    $this->_helper->viewRenderer->setNoRender ();	
	}
}
?>
